﻿//===============================================================================
// Microsoft patterns & practices
// CompositeUI Application Block
//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

// The example companies, organizations, products, domain names, e-mail addresses,
// logos, people, places, and events depicted herein are fictitious.  No association
// with any real company, organization, product, domain name, email address, logo,
// person, places, or events is intended or should be inferred.
using System;
using System.Collections;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Utility;

using DevExpress.XtraEditors;
using System.Windows.Forms;

using BusinessEntity.Common;
using BusinessEntity.FileManagement;
using Common;
using Service.Interface;
using Client.BusinessAction;

namespace Module.Extension.FileManagerModule.Services
{
    [Service]
    public class FileManagerService
    {       
        public FileManagerService()
        {
        }

        public SortableCollection<MyFile> Load(MyFile oSearchMyFile)
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                SortableCollection<MyFile> oMyFiles = patientService.Proxy.SearchFile(UtilityClient.GetSessionId(), oSearchMyFile, out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                    return null;
                }

                return oMyFiles;

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            } 
        }

        public void UploadFile(MyFile oMyFile)
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                patientService.Proxy.UploadFile(UtilityClient.GetSessionId(), oMyFile, out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                }
                else
                {
                    XtraMessageBox.Show("File uploaded successfully.");
                }

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        public void DeleteFile(int fileId)
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                patientService.Proxy.DeleteFile(UtilityClient.GetSessionId(), fileId, out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                }
                else
                {
                    XtraMessageBox.Show("File Deleted successfully.");
                }

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }


        public void Save(SortableCollection<MyFile> oFiles)
        {
            try
            {
                //Call WCF Service to 
                //ADD YOUR LOGIC

                XtraMessageBox.Show("Save , call WCF Service to save");
               
            }
            catch (Exception ex)
            {
                //Don't chage this part
                XtraMessageBox.Show(Utility.GetExceptionMessageString(ex), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
        }


        public SortableCollection<MyFileType> LoadFileType()
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                SortableCollection<MyFileType> oMyFiles = patientService.Proxy.GetFileType(UtilityClient.GetSessionId(), out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                    return null;
                }

                return oMyFiles;

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public SortableCollection<DocumentType> LoadDocumentType()
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                SortableCollection<DocumentType> DocTypes = patientService.Proxy.GetDocumentType(UtilityClient.GetSessionId(), out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                    return null;
                }

                return DocTypes;

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public SortableCollection<CoreUser> LoadCoreUsers()
        {
            string errorMsg = string.Empty;
            try
            {
                string successMessage = string.Empty;
                string errorMessage = string.Empty;

                GenericClientProxy<IFileManagementService> patientService
                           = new GenericClientProxy<IFileManagementService>(ServiceNames.FileManagementService);

                SortableCollection<CoreUser> oCoreUsers = patientService.Proxy.GetCoreUsers(UtilityClient.GetSessionId(), out successMessage, out errorMessage);

                if (errorMessage.Length > 0)
                {
                    XtraMessageBox.Show(errorMessage);
                    return null;
                }

                return oCoreUsers;

            }
            catch (Exception e)
            {
                errorMsg = Utility.GetExceptionMessageString(e);
                Utility.LogEmail(e);
                XtraMessageBox.Show(errorMsg, Utility.GetErrorMessageTitle(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
    }
}